package com.sunny.ynhz.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.foamtrace.photopicker.SelectModel;
import com.sunny.ynhz.R;
import com.sunny.ynhz.api.NetUrl;
import com.sunny.ynhz.ui.ImageActivity;
import com.sunny.ynhz.customphoto.CustomPhotoPickerIntent;

import java.util.ArrayList;

/**
 * Created by luao on 2017/8/18.
 */


public class ImageMoreStyleAdapter extends RecyclerView.Adapter<ImageMoreStyleAdapter.ViewHolder> {

    private ArrayList<String> data = null;
    private Context context;
    public ImageMoreStyleAdapter(Context context, ArrayList<String> data) {
        this.data = data;
        this.context = context;

    }
    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_pro_img, viewGroup, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        //取data的时候防止数组越界
        if (position == data.size()) {
            viewHolder.del_image.setVisibility(View.GONE);
            viewHolder.bg_img.setVisibility(View.GONE);
            Glide.with(context)
                    .load(R.mipmap.icon_add_image)
                    .centerCrop()
                    .into(viewHolder.image);
            viewHolder.content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CustomPhotoPickerIntent intent = new CustomPhotoPickerIntent(context);
                    intent.setSelectModel(SelectModel.MULTI);
                    intent.setShowCarema(true); // 是否显示拍照， 默认false
                    intent.setMaxTotal(9); // 最多选择照片数量，默认为9
                    intent.setSelectedPaths(data); // 已选中的照片地址， 用于回显选中状态
                    ((Activity) context).startActivityForResult(intent, 0);
                }
            });
        } else {
            String string = data.get(position);
            String imageUrl = "";
            if (string.indexOf("FileUploadpath") != -1) {
                imageUrl = NetUrl.imageBaseUrl + string;
            } else {
                imageUrl = string;
            }
            viewHolder.bg_img.setVisibility(View.VISIBLE);
            viewHolder.del_image.setVisibility(View.VISIBLE);
            Glide.with(context)
                    .load(imageUrl)
                    .error(R.mipmap.xwdt)
                    .centerCrop()
                    .into(viewHolder.image);
            final String finalImageUrl = imageUrl;
            viewHolder.content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, ImageActivity.class);
                    intent.putExtra("pic_path", finalImageUrl);
                    if (Build.VERSION.SDK_INT < 21) {
                        context.startActivity(intent);
                    } else {
                        ActivityOptionsCompat options = ActivityOptionsCompat.
                                makeSceneTransitionAnimation((Activity) context, view, "shareAnimTexView");
                        context.startActivity(intent, options.toBundle());
                    }
                }
            });

            viewHolder.del_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    data.remove(position);
                    notifyDataSetChanged();
                }
            });
        }
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return data.size() + 1;
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView image;
        public ImageView del_image;
        public RelativeLayout content;
        View bg_img;

        public ViewHolder(View view) {
            super(view);
            image = (ImageView) view.findViewById(R.id.image);
            del_image = (ImageView) view.findViewById(R.id.del_image);
            content = (RelativeLayout) view.findViewById(R.id.content);
            bg_img=view.findViewById(R.id.img_bg);
        }
    }
}